home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / as5.zip / SYMTAB.C < prev    next >
C/C++ Source or Header  |  1987-12-09  |  3KB  |  142 lines

  1. /*
  2.  *      install --- add a symbol to the table
  3.  */
  4. install(str,val)
  5. char    *str;
  6. int     val;
  7. {
  8.         struct link *lp;
  9.     struct nlist *np,*p,*backp;
  10.     struct nlist *lookup();
  11.     int     i;
  12.  
  13.     if( !alpha(*str) ){
  14.         error("Illegal Symbol Name");
  15.         return(NO);
  16.         }
  17.     if( (np = lookup(str)) != NULL ){
  18.         if( Pass==2 ){
  19.             if( np->def == val )
  20.                 return(YES);
  21.             else{
  22.                 error("Phasing Error");
  23.                 return(NO);
  24.                 }
  25.             }
  26.         error("Symbol Redefined");
  27.         return(NO);
  28.         }
  29.     /* enter new symbol */
  30. #ifdef DEBUG
  31.     printf("Installing %s as %d\n",str,val);
  32. #endif
  33.     np = (struct nlist *) alloc(sizeof(struct nlist));
  34.     if( np == (struct nlist *)ERR ){
  35.         error("Symbol table full");
  36.         return(NO);
  37.         }
  38.     np->name = alloc(strlen(str)+1);
  39.     if( np->name == (char *)ERR ){
  40.         error("Symbol table full");
  41.         return(NO);
  42.         }
  43.     strcpy(np->name,str);
  44.     np->def = val;
  45.     np->Lnext = NULL;  
  46.         np->Rnext = NULL;
  47.            lp = (struct link *) alloc(sizeof(struct link));
  48.            np->L_list = lp;
  49.            lp->L_num = Line_num;
  50.            lp->next = NULL;
  51.         p = root;
  52.           backp = NULL;
  53.            while (p != NULL) 
  54.             {
  55.               backp = p;
  56.               i = strcmp (str,p->name);
  57.                if (i<0)
  58.                    p=p->Lnext;
  59.                   else p=p->Rnext;
  60.             }
  61.           if (backp == NULL)
  62.               root = np;
  63.              else if (strcmp(str,backp->name)<0)
  64.                   backp->Lnext = np;
  65.                  else backp->Rnext = np;
  66.           return (YES);  
  67. }
  68.  
  69. /*
  70.  *      lookup --- find string in symbol table
  71.  */
  72. struct nlist *
  73. lookup(name)
  74. char    *name;
  75. {
  76.     struct nlist *np;
  77.     int     i;
  78.  
  79.         np = root;
  80.          while (np != NULL)
  81.           {
  82.             i = strcmp(name,np->name);
  83.              if (i == 0)
  84.                {
  85.                  Last_sym = np->def;
  86.                  return (np);
  87.                }
  88.              else if (i < 0)
  89.                  np = np->Lnext;
  90.                 else np = np->Rnext;
  91.           }
  92.       Last_sym = 0;
  93.       if (Pass == 2)
  94.           error ("symbol Undefined on pass 2");
  95.         return (NULL); 
  96. }
  97.  
  98.  
  99. #define NMNE (sizeof(table)/ sizeof(struct oper))
  100. #define NPSE (sizeof(pseudo)/ sizeof(struct oper))
  101. /*
  102.  *      mne_look --- mnemonic lookup
  103.  *
  104.  *      Return pointer to an oper structure if found.
  105.  *      Searches both the machine mnemonic table and the pseudo table.
  106.  */
  107. struct oper *
  108. mne_look(str)
  109. char    *str;
  110. {
  111.     struct oper *low,*high,*mid;
  112.     int     cond;
  113.  
  114.     /* Search machine mnemonics first */
  115.     low =  &table[0];
  116.     high = &table[ NMNE-1 ];
  117.     while (low <= high){
  118.         mid = low + (high-low)/2;
  119.         if( ( cond = strcmp(str,mid->mnemonic)) < 0)
  120.             high = mid - 1;
  121.         else if (cond > 0)
  122.             low = mid + 1;
  123.         else
  124.             return(mid);
  125.     }
  126.  
  127.     /* Check for pseudo ops */
  128.     low =  &pseudo[0];
  129.     high = &pseudo[ NPSE-1 ];
  130.     while (low <= high){
  131.         mid = low + (high-low)/2;
  132.         if( ( cond = strcmp(str,mid->mnemonic)) < 0)
  133.             high = mid - 1;
  134.         else if (cond > 0)
  135.             low = mid + 1;
  136.         else
  137.             return(mid);
  138.     }
  139.  
  140.     return(NULL);
  141. }
  142.